#pragma once
#include <iostream>
#include <unordered_map>
#include <string>
#include <fstream>
#include <string.h>
#include "Logger.hpp"
#include "Common.hpp"

namespace DicMudule
{
    using namespace LogMudule;


    const std::string defpath = "./";
    const std::string defname = "Dic.txt";
    const std::string gap = ":";
    class Dictionary
    {
        bool LoadDictionary()
        {
            std::string filename = _path + _name;
            std::fstream in(filename);
            if(!in.is_open())
            {
                LOG(LogLevel::FATAL)<<"open file";
                return false;
            }
            std::string line;
            while(getline(in,line))
            {
                std::string key;
                std::string value;
                if(SplitString(line,&key,&value,gap))
                {
                    _dictionary.insert(std::make_pair(key, value));
                }
            }
            in.close();
            return true;
        }
    public:
        Dictionary(std::string path = defpath, std::string name = defname)
            : _path(path),
              _name(name)
        {
            if(!LoadDictionary()) return;
        }
        std::string Translate(const std::string &word)
        {
            auto iter = _dictionary.find(word);
            if(iter == _dictionary.end()) return "None";
            return iter->second;
        }

    private:
        std::unordered_map<std::string, std::string> _dictionary;
        std::string _path;
        std::string _name;
    };
}
